package org.leviatan.dataharbour.gui.listeners;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import org.leviatan.dataharbour.core.model.HarbourRequest;
import org.leviatan.dataharbour.gui.RuntimeContext;
import org.leviatan.dataharbour.gui.swingmodels.TreeNodeMdlHarbourRequest;

public class RequestTreeListener implements TreeSelectionListener {
	
	private JTree theTree = null;

	public RequestTreeListener(JTree aTree) {
		this.theTree = aTree;
	}
	
	public void valueChanged(TreeSelectionEvent e) {
		DefaultMutableTreeNode selectedNode = 
			(DefaultMutableTreeNode) theTree.getLastSelectedPathComponent();

		if (selectedNode == null) return;
	
		Object nodeModel = selectedNode.getUserObject();
		
		
		if (selectedNode instanceof TreeNodeMdlHarbourRequest) {
			
			
			String reqId = ((TreeNodeMdlHarbourRequest) selectedNode).getId();
			
			System.out.println("request model id: " + reqId);
			
			HarbourRequest reqMdl = 
				RuntimeContext.dataHarbourService.getUnpersistedHarbourRequest(reqId);
			
			//System.out.println(Register.getInstance().getHarbourRequestModels());
			RuntimeContext.harbouRequestDetailView.setModel(reqMdl);	
		}
		
		
	}

}
